package com.huike.controller;

import com.huike.po.Part;
import com.huike.po.p2pMessage;
import com.huike.pojo.Response;
import com.huike.service.PartService;
import com.huike.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
/**
 * 部门相关操作
 * 1、增加部门: addPart,需要传入partName（部门名称）,managerId（部门经理的编号）以及partLevel(部门等级)
 * 2、删除部门：deletePart,须传入partId(部门编号)
 * 3、修改部门：updatePart,须传入part（Part类，表示修改后的部门信息）
 * 4、查询部门
 *  （1）获取所有部门信息：getAllPartInfo
 *  （2）按部门名称模糊查询：getPartByName,须传入partName（部门名称关键词，如“业务组”）
 *  （3）通过部门编号查询：getPartById,须传入partId（部门编号）
 */
@RequestMapping("/part")
public class PartController {
    @Autowired
    private PartService partService;
    @Autowired
    private Response response;

    //增
    @RequestMapping(value = "/addPart", method=RequestMethod.POST)
    @ResponseBody
    public Response addPart(@RequestBody Part part, HttpSession session) {
        String partName = part.getPartName();
        int managerId = part.getManagerId();
        int partLevel = part.getPartLevel();
        int partId = part.getPartId();
        boolean res = partService.addPart(partId,partName, managerId, partLevel);
        if(res) {
            response.setCode("200");
            response.setContent("添加部门成功");
        }
        else {
            response.setCode("400");
            response.setContent("添加部门失败");
        }
        return response;
    }
    @RequestMapping(value = "/deletePart")
    @ResponseBody
    public Response deletePart(int partId, HttpSession session) {
        if (session.getAttribute("loginUser") == null) {
            response.setCode("400");
            response.setContent("请先登录");
        } else {
            boolean res = partService.deletePart(partId);
            if(res) {
                response.setCode("200");
                response.setContent("删除部门成功");
            }
            else {
                response.setCode("400");
                response.setContent("删除部门失败");
            }
        }
        return response;
    }
    @RequestMapping(value = "/updatePart", method=RequestMethod.POST)
    @ResponseBody
    public Response updatePart(@RequestBody Part part, HttpSession session) {
        if (session.getAttribute("loginUser") == null) {
            response.setCode("400");
            response.setContent("请先登录");
        } else {
            boolean res = partService.updatePart(part);
            if(res) {
                response.setCode("200");
                response.setContent("修改成功");
            }
            else {
                response.setCode("400");
                response.setContent("修改失败");
            }
        }
        return response;
    }
    @RequestMapping(value = "/getAllPartInfo")
    @ResponseBody
    public Response getAllPartInfo(HttpSession session) {
        List<Part> partList = partService.getPartList();
        if(partList != null) {
            response.setCode("200");
            response.setContent(GsonUtils.getGson().toJson(partList));
        }
        else {
            response.setCode("400");
            response.setContent("获取列表失败");
        }
        return response;
    }
    @RequestMapping(value = "/getPartByName")
    @ResponseBody
    public Response getPartByName(String partName, HttpSession session) {
        if (session.getAttribute("loginUser") == null) {
            response.setCode("400");
            response.setContent("请先登录");
        } else {
            List<Part> partList = partService.getPartListByName(partName);
            if(partList != null) {
                response.setCode("200");
                response.setContent(GsonUtils.getGson().toJson(partList));
            }
            else {
                response.setCode("400");
                response.setContent("获取列表失败");
            }
        }
        return response;
    }
    @RequestMapping(value = "/getPartById")
    @ResponseBody
    public Response getPartById(int partId, HttpSession session) {
        if (session.getAttribute("loginUser") == null) {
            response.setCode("400");
            response.setContent("请先登录");
        } else {
            Part part = partService.getPartById(partId);
            if(part != null) {
                response.setCode("200");
                response.setContent(GsonUtils.getGson().toJson(part));
            }
            else {
                response.setCode("400");
                response.setContent("获取列表失败");
            }
        }
        return response;
    }

    @RequestMapping(value = "/addEmployeeToPart")
    @ResponseBody
    public Response addEmployeeToPart(int partId, int employeeId, HttpSession session) {
        if (session.getAttribute("loginUser") == null) {
            response.setCode("400");
            response.setContent("请先登录");
        } else {
            boolean res = partService.addEmployeeToPart(partId, employeeId);
            if(res) {
                response.setCode("200");
                response.setContent("添加员工成功");
            }
            else {
                response.setCode("400");
                response.setContent("添加员工失败");
            }
        }
        return response;
    }
    @RequestMapping(value = "/deleteEmployeeFromPart")
    @ResponseBody
    public Response deleteEmployeeFromPart(int employeeId, HttpSession session) {
        if (session.getAttribute("loginUser") == null) {
            response.setCode("400");
            response.setContent("请先登录");
        } else {
            boolean res = partService.deleteEmployeeFromPart(employeeId);
            if(res) {
                response.setCode("200");
                response.setContent("删除员工成功");
            }
            else {
                response.setCode("400");
                response.setContent("删除员工失败");
            }
        }
        return response;
    }
}
